package com.ccrfid.rmc6.service.business.visitroom;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.business.visitroom.RoomDutyVisitorEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelView;
import com.ccrfid.rmc6.repository.business.visitroom.RoomDutyVisitorRepository;
import com.ccrfid.rmc6.repository.personnel.PersonnelViewRepository;
import com.ccrfid.rmc6.repository.sys.OrgRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RoomDutyVisitorServiceImpl implements RoomDutyVisitorService {
    private static final Logger logger = LoggerFactory.getLogger(RoomVisitRuleServiceImpl.class);
    @Autowired
    private RoomDutyVisitorRepository roomDutyVisitorRepository;
    @Autowired
    private PersonnelViewRepository personnelViewRepository;
    @Autowired
    private OrgRepository orgRepository;

    @Override
    public List<PersonnelView> getRoomDutyVisitors(Long orgId) {
        List<RoomDutyVisitorEntity> visitors = roomDutyVisitorRepository.getAllByOrgId(orgId);
        if (visitors != null && visitors.size() > 0) {
            List<PersonnelView> list = new ArrayList<>();
            for (RoomDutyVisitorEntity visitorEntity : visitors) {
                list.add(personnelViewRepository.getById(visitorEntity.getPersonnelId()));
            }
            return list;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public PersonnelView updateRoomDutyVisitor(Long orgId, Long personnelId) throws AppException {
        if (orgId == null || !orgRepository.existsById(orgId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.org_not_found.getCode(), CustomErrors.org_not_found.getReason(), "");
        }
        if (personnelId == null || !personnelViewRepository.existsById(personnelId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.person_not_found.getCode(), CustomErrors.person_not_found.getReason(), "");
        }
        RoomDutyVisitorEntity visitorEntity = roomDutyVisitorRepository.getByOrgIdAndPersonnelId(orgId, personnelId);
        if (visitorEntity == null) {
            visitorEntity = new RoomDutyVisitorEntity();
            visitorEntity.setOrgId(orgId);
            visitorEntity.setPersonnelId(personnelId);
            roomDutyVisitorRepository.save(visitorEntity);
        }
        PersonnelView personnelView = personnelViewRepository.getById(personnelId);
        return personnelView;
    }

    @Override
    public Boolean deleteRoomDutyVisitor(Long orgId, Long personnelId) throws AppException {
        if (!roomDutyVisitorRepository.existsByOrgIdAndPersonnelId(orgId, personnelId)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        } else {
            roomDutyVisitorRepository.deleteByOrgIdAndPersonnelId(orgId, personnelId);
        }
        return true;
    }
}
